package com.zhidian.edm.util;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.model.ClassAnnotationAttributes;

import java.util.Collections;

public class CodeGenerator {

    public static void main(String[] args) {
        // 数据库连接信息
        String url = "jdbc:mysql://127.0.0.1:3306/edm?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&characterEncoding=utf-8";
        String username = "root";
        String password = "f9$Tq7@vK3zP!wR1s8mL";

        FastAutoGenerator.create(url, username, password)
                // 全局配置
                .globalConfig(builder -> builder
                        .disableOpenDir()
                        .author("sl")
                        .outputDir(System.getProperty("user.dir") + "/src/main/java") // 输出路径
                        .commentDate("yyyy-MM-dd") //  设置注释日期格式
                        .dateType(DateType.TIME_PACK) // 时间类型使用 java.time
                )
                // 包配置
                .packageConfig(builder -> builder
                        .parent("com.zhidian.edm")
                        .entity("entity")
                        .mapper("mapper")
                        .service("service")
                        .serviceImpl("service.impl")
                        .controller("controller")
                        .pathInfo(Collections.singletonMap(OutputFile.xml,
                                System.getProperty("user.dir") + "/src/main/resources/mapper")) // XML输出路径
                )
                // 策略配置
                .strategyConfig(builder -> builder
                        // 表名匹配
                        .addInclude("sys_menu","sys_role_menu")
                        .entityBuilder()
                        .formatFileName("%sPO")
                        .enableLombok(new ClassAnnotationAttributes("@Data","lombok.Data"))          // 使用 Lombok
                        .idType(IdType.AUTO)       // 主键策略
                        .mapperBuilder()
                        .enableBaseResultMap()     // XML生成BaseResultMap
                        .enableBaseColumnList()    // XML生成BaseColumnList
                        .controllerBuilder()
                        .enableRestStyle()         // 生成@RestController
                        .enableFileOverride()
                        .build()
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}